<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
	<title></title>
	<link rel="stylesheet" href="/public/css/common.css">
	<link rel="stylesheet" href="/public/layui/css/layui.css">
	<script src="/public/js/jquery.min.js"></script>
	<script src="/public/js/xadmin.js"></script>
	<!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
	<!--[if lt IE 9]>
	  <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
	  <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
	<![endif]-->
</head>
<body>
	<div class="page-content layui-anim layui-anim-upbit">
		<h2>用户管理</h2>
		<!-- 内容搜索栏 -->
		<div class="yg-search">
			<form class="layui-form" action="/admin/search_user" method="POST">
				<input type="text" name="ccount" lay-verify="ccount" placeholder="请输入用户账号" autocomplete="off" class="layui-input">
				<button class="layui-btn" lay-submit="" lay-filter="sreach">
					<i class="layui-icon">&#xe615;</i>
				</button>
			</form>
		</div>
		
		<!-- 操作栏 -->
		<div class="yg-block">
			<div>
				<button class="layui-btn layui-btn-danger" onclick="delAll()">
					<i class="layui-icon">&#xe640;</i>
					<span>批量删除</span>
				</button>
				<button class="layui-btn" onclick="x_admin_show('添加用户','/admin/add_user',600,400)">
					<i class="layui-icon">&#xe61f;</i>
					<span>添加</span>
				</button>
			</div>
			<span class="x-right" style="line-height:40px">共有数据：<%= userList.length %> 条</span>
		</div>
		
		<!-- 列表栏 -->
		<table class="layui-table">
		  <thead>
		    <tr>
		      <th>
		        <div class="layui-unselect header layui-form-checkbox" lay-skin="primary">
					<i class="layui-icon">&#xe605;</i>
				</div>
		      </th>
		      <th>ID</th>
		      <th>账号</th>
		      <th>用户名</th>
		      <th>性别</th>
		      <th>加入时间</th>
		      <th >操作</th>
		      </tr>
		  </thead>
		  <tbody>
				{{each userList}}
		    <tr>
		      <td>
		        <div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id='<%= $value.id %>'>
							<i class="layui-icon">&#xe605;</i>
						</div>
					</td>
		      <td>{{$value.id}}</td>
		      <td>{{$value.ccount}}</td>
		      <td>{{$value.name}}</td>
					{{if $value.sex == 0}}
					<td>女</td>
					{{else}}
					<td>男</td>
					{{/if}}
		      <td>{{$value.add_time}}</td>
		      <td class="td-manage">
		        <a title="查看编辑"  onclick="x_admin_show('编辑','/admin/update_user/<%= $value.id %>',600,400)" href="javascript:;">
		          <i class="layui-icon">&#xe642;</i>
		        </a>
		        <a title="删除" onclick="del_user(this,'<%= $value.id %>','<%= $value.ccount %>')" href="javascript:;">
		          <i class="layui-icon">&#xe640;</i>
		        </a>
					</td>
		    </tr>
				{{/each}}
		  </tbody>
		</table>
		
		<!-- 分页栏 -->
		<% if(all_page > 1) { %>
		<div class="yg-paging">
			<!-- 上一页 -->
			<% if(current_page > 1) { %>
			<a class="prev" href="/admin/user/<%= current_page - 1 %>">&lt;</a>
			<% } %>
			<!-- 上一页 end -->
			
			<!-- 第一页 -->
			<% if(current_page == 1){ %>
				<a class="current" href="/admin/user/1">1</a>
			<% } else {%>
				<a class="num" href="/admin/user/1">1</a>
			<% }%>
			<!-- 第一页 end -->

			<!-- 当页数大于7的时候 -->
			<% if(all_page > 7){ %>
				<!-- 前省略号 -->
				<% if(current_page - 3 > 1) { %>
					<a class="omit" href="javascript:;">...</a>
				<% } else { %>
					<% for(var i = 2; i <= 6; i++){ %>
						<% if(i == current_page){ %>
						<a class="current" href="/admin/user/<%= i %>"><%= i %></a>
						<% } else {%>
						<a class="num" href="/admin/user/<%= i %>"><%= i %></a>
						<% }%>
					<% } %>
				<% } %>
				<!-- 前省略号 end -->

				<!-- 页码 -->
				<% if(current_page - 3 > 1 && current_page + 3 < all_page){ %>
					<a class="num" href="/admin/user/<%= current_page - 2 %>"><%= current_page - 2 %></a>
					<a class="num" href="/admin/user/<%= current_page - 1 %>"><%= current_page - 1 %></a>
					<a class="current" href="/admin/user/<%= current_page %>"><%= current_page %></a>
					<a class="num" href="/admin/user/<%= current_page + 1 %>"><%= current_page + 1 %></a>
					<a class="num" href="/admin/user/<%= current_page + 2 %>"><%= current_page + 2 %></a>
				<% } %>
				<!-- 页码 end -->

				<!-- 后省略号 -->
				<% if(current_page + 3 < all_page) { %>
					<a class="omit" href="javascript:;">...</a>
				<% } else { %>
					<% for(var i = all_page - 5; i <= all_page - 1; i++){ %>
						<% if(i == current_page){ %>
						<a class="current" href="/admin/user/<%= i %>"><%= i %></a>
						<% } else {%>
						<a class="num" href="/admin/user/<%= i %>"><%= i %></a>
						<% }%>
					<% } %>
				<% } %>
				<!-- 后省略号 end -->
			<% } else{ %>
				<% for(var i = 2; i <= all_page - 1; i++){ %>
					<% if(i == current_page){ %>
					<a class="current" href="/admin/message/<%= i %>"><%= i %></a>
					<% } else {%>
					<a class="num" href="/admin/message/<%= i %>"><%= i %></a>
					<% }%>
				<% } %>
			<% } %>
				
			<!-- 最后一页 -->
			<% if(current_page == all_page){ %>
				<a class="current" href="/admin/user/<%= all_page %>"><%= all_page %></a>
			<% } else {%>
				<a class="num" href="/admin/user/<%= all_page %>"><%= all_page %></a>
			<% }%>
			<!-- 最后一页 end -->
			<!-- 下一页 -->
			<% if(all_page > current_page) { %>
			<a class="prev" href="/admin/user/<%= current_page + 1 %>">&gt;</a>
			<% } %>
			<!-- 下一页 end -->
			
			<!-- 跳到第几页 -->
			<div class="to-page">
				<p>共<%= index %>条 </p>
				<p> 到第</p>
				<input type="text" id="to_page" value="<%= current_page %>">
				<button onclick="toPage()">确定</button>
			</div>
			<!-- 跳到第几页 end -->
		</div>
		<% } %>
		<!-- 分页栏 end -->
	</div>
</body>
<script src="/public/layui/layui.js"></script>
<script src="/public/js/api.js"></script>
<script>
	// 跳转页码
	function toPage(){
		let val = parseInt($("#to_page").val());
		if(val == NaN){
			layer.msg('输入页码有误');
		} else {
			console.log(parseInt('<%= all_page %>'),'/admin/user/<%= all_page %>','/admin/user/' + val)
			if (val >= parseInt('<%= all_page %>')){
				window.location.href = '/admin/user/<%= all_page %>';
			} else if(val <= 1) {
				window.location.href = '/admin/user/1';
			} else {
				window.location.href = '/admin/user/' + val;
			}
		} 
	}


	function del_user(obj,id,ccount) {
		layer.confirm('确认要删除该用户吗？',function(index){
			_http.del_user({
				id: id
			}, (res) => {
				if(res.code === 0){
					layer.msg(res.msg,{icon:1,time:1000});
					setTimeout(() => {
						location.reload()
					},1000)
				} else {
					layer.msg(res.msg,{icon:5,time:1000});
				}
			})
		});
	}

	// 批量删除
	function delAll (argument) {
		var data = tableCheck.getData();
		if(data.length !== 0){
			layer.confirm('确认要删除选中的用户吗？', function(index){
				_http.del_more({
					data: data.join(',')
				}, (res) => {
					console.log(res);
					if(res.code === 0) {
						layer.msg(res.msg, {icon: 1});
						setTimeout(() => {
							location.reload();
						}, 1000)
					} else {
						layer.msg(res.msg, {icon: 2});
					}
				})
			});
		} else {
			layer.msg('请勾选需要删除项', {icon: 7});
		}
	}
</script>
</html>
